
figure_1 <-
  estimation_data %>%
  filter(!is.na(validated_election_outcome)) %>%
  mutate(elected = as.factor(validated_election_outcome)) %>%
  select(ID, A, elected, starts_with("avg_score")) %>%
  pivot_longer(cols = avg_score_democ_support) %>%
  filter(name %in% c("avg_score_democ_support")) %>%
  mutate(name = case_when(name == "avg_score_democ_support" ~ "Support Democracy")) %>%
  group_by(A, elected, name) %>%
  summarise(val = mean(value, na.rm = T),
            sd = sd(value, na.rm = T),
            n_val = n()) %>%
  mutate(se_val = sd/sqrt(n_val)) %>%
  ggplot(aes(x=A, y = val, group = elected, color = elected)) +
  #geom_point(position = position_dodge(width = 0.05), shape = 21) +
  geom_errorbar(aes(ymin=val - se_val*1.96, ymax = val + se_val*1.96), width = 0, position = position_dodge(width = 0.05)) +
  geom_pointpath(size = 1, mult = .5, position = position_dodge(width = 0.05)) +
  #geom_line(position = position_dodge(width = 0.1)) +
  theme_bw() +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        #legend.position = "none",
        axis.title.x = element_blank(),
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        legend.position = "bottom") +
  scale_color_manual(values = c("black", "darkgrey")) +
  geom_vline(xintercept = 2.2, linetype = "dashed", color = "black") +
  scale_x_continuous(breaks = c(1, 2, 3),
                     labels = c("Wave 1\n(Nov. 2023)", "Wave 2\n(Jan. 2024)", "Wave 3\n(Apr. 2024)")) +
  ylab("Support for Democracy\n(Index, 1-4)") +
  coord_cartesian(ylim = c(2.95, 3.3))


#trust elections
figure_2 <-
  estimation_data %>%
  filter(!is.na(validated_election_outcome)) %>%
  mutate(elected = as.factor(validated_election_outcome)) %>%
  select(ID, A, elected, starts_with("avg_score")) %>%
  pivot_longer(cols = avg_score_trust_elect) %>%
  filter(name %in% c("avg_score_trust_elect")) %>%
  group_by(A, elected, name) %>%
  summarise(val = mean(value, na.rm = T),
            sd = sd(value, na.rm = T),
            n_val = n()) %>%
  mutate(se_val = sd/sqrt(n_val)) %>%
  ggplot(aes(x=A, y = val, group = elected, color = elected)) +
  geom_errorbar(aes(ymin=val - se_val*1.96, ymax = val + se_val*1.96), width = 0, position = position_dodge(width = 0.05)) +
  geom_pointpath(size = 1, mult = .5, position = position_dodge(width = 0.05)) +
  theme_bw() +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        #legend.position = "none",
        axis.title.x = element_blank(),
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        legend.position = "bottom") +
  scale_color_manual(values = c("black", "darkgrey")) +
  geom_vline(xintercept = 2.2, linetype = "dashed", color = "black") +
  scale_x_continuous(breaks = c(1, 2, 3),
                     labels = c("Wave 1\n(Nov. 2023)", "Wave 2\n(Jan. 2024)", "Wave 3\n(Apr. 2024)")) +
  ylab("Trust in Elections\n(Index, 1-4)") +
  coord_cartesian(ylim = c(2.6, 2.95)) # limit is .35 on y axis



ggsave(filename = "./outputs/figures/figure_1a.pdf", plot = figure_1, width = 5, height = 4)
ggsave(filename = "./outputs/figures/figure_1b.pdf", plot = figure_2, width = 5, height = 4)

ggsave(filename = "./outputs/figures/figure_1a.eps", plot = figure_1, width = 5, height = 4, device = "eps")
ggsave(filename = "./outputs/figures/figure_1b.eps", plot = figure_2, width = 5, height = 4, device = "eps")



